home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15221 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  84 lines

  1. Path: news.bu.edu!engc!jayaram
  2. From: jayaram@engc.bu.edu (J. Ram)
  3. Newsgroups: comp.lang.c
  4. Subject: Pascal -> C conversion
  5. Date: 17 Apr 1996 21:54:43 GMT
  6. Organization: Boston University
  7. Message-ID: <4l3pb3$skd@news.bu.edu>
  8. NNTP-Posting-Host: engc.bu.edu
  9. X-Newsreader: TIN [version 1.2 PL0]
  10.  
  11.  
  12. Hello netters:
  13.     
  14.     I have a simple 23 line pascal program I wrote 5 years ago that 
  15. I'd like to CONVERT to C. Can someone help since I'm not well 
  16. versed with the C syntax. All that needs to be done is change of syntax
  17. without any change in programming style or approach to the problem.
  18.  
  19. This pascal program uses a simple circular linked lists to solve the "Josephus
  20. Problem".  
  21.  
  22. Start of program
  23.  
  24. ----------------------------------------------------------------------------
  25. August 25, 1991
  26.  
  27. The program is a short program that solves the "Josephus Problem"
  28. For the Josephus problem, we can imagine that N people have decided to commit
  29. mass suicide by arranging themselves in circle and killing the M th person 
  30. around the circle, closing ranks as each person drops out of the circle.
  31. The problem is to find out which person is the last to die or more importantly
  32. to find the order in which the people were executed.
  33.  
  34. Example
  35.        for N=9 and M=5, the order of execution is 5,1,7,4,3,6,9,2,8
  36.  
  37. --------------------------PASCAL PROGRAM  -----
  38. PROGRAM josephus(input,output);
  39. type link = ^node;
  40.     node = RECORD key:integer;
  41.               next:link
  42.                END;
  43. var  i,N,M:integer;
  44.        t,x:link;     
  45. begin
  46.     read (N,M);  
  47.     new(t);  t^.key:=1; x:=t;
  48.     for i:=2 to N do
  49.       begin
  50.          new(t^.next);    t:=t^.next;  t^.key:= i
  51.           end;
  52.         t^.next:=x;
  53.      while t <> t^.next do
  54.           begin
  55.         for i:=1 to M-1 do t:=t^.next;
  56.         write(t^.next^.key);
  57.         x:=t^.next;
  58.         t^.next:=t^.next^.next;
  59.         dispose(x);
  60.       end;
  61.        writeln(t^.key);
  62. end
  63.  
  64. -------end program-------------------------------
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.